package com.jinyuan.options.login;

import com.jinyuan.R;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

/**
 * 登陆顶部菜单控制
 * @author azuryleaves
 *
 */
public class LoginTopFragment extends Fragment {
	
	private Callbacks callbacks = defaultCallbacks;
	private RadioGroup login_tab_RadioGroup;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		super.onCreateView(inflater, container, savedInstanceState);
		View view = inflater.inflate(R.layout.login_tab_top, container, false);
		login_tab_RadioGroup = (RadioGroup) view.findViewById(R.id.login_tab_RadioGroup);
		((RadioButton) login_tab_RadioGroup.getChildAt(0)).setChecked(true);
		login_tab_RadioGroup.setOnCheckedChangeListener(checkedChangeListener);
		return view;
	}

	private OnCheckedChangeListener checkedChangeListener = new OnCheckedChangeListener() {
		
		@Override
		public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
			callbacks.onRadioGroupCheckedChanged(radioGroup, checkedId);
		}
	};
	
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		if (!(activity instanceof Callbacks)) {
			throw new IllegalStateException(
					"Activity must implement fragment's callbacks.");
		}
		callbacks = (Callbacks) activity;
	}

	@Override
	public void onDetach() {
		super.onDetach();
		callbacks = defaultCallbacks;
	}

	public interface Callbacks {
		
		/**
		 * 顶部菜单RadioGroup点击事件回调接口
		 * @param radioGroup 
		 * @param checkedId 菜单子项ID
		 */
		public void onRadioGroupCheckedChanged(RadioGroup radioGroup, int checkedId);
		
	}

	private static Callbacks defaultCallbacks = new Callbacks() {

		@Override
		public void onRadioGroupCheckedChanged(RadioGroup radioGroup,
				int checkedId) {
			
		}

	};
}
